package com.cgsd.heyu.ui.news;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;

import com.apkfuns.logutils.LogUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.presenter.news.NewsContract;
import com.cgsd.heyu.presenter.news.NewsPresenter;
import com.cgsd.heyu.presenter.user.UserPresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.hztz.kankanzhuan.event.TaskStatusEvent;
import com.hztz.kankanzhuan.view.BannerKanView;
import com.hztz.kankanzhuan.view.KanNewsView;
import com.jeremyliao.liveeventbus.LiveEventBus;

/**
 * author:
 * time:2021/4/13
 * description:xin wen zhuan
 */
public class NewsActivity extends BaseActivity<NewsContract.Presenter> implements NewsContract.NewsView {

    private KanNewsView mNewsView;
    private BannerKanView mBannerView;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, NewsActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_news;
    }

    @Override
    protected void initView() {
        new NewsPresenter(this);
        mNewsView = findViewById(R.id.kanView);
        mBannerView = findViewById(R.id.banner_view);

    }

    @Override
    protected void initData() {
        Lifecycle lifecycle = getLifecycle();
        mNewsView.setObserve(this);
        lifecycle.addObserver(mNewsView);
        lifecycle.addObserver(mBannerView);

        //任务执行状态
        LiveEventBus.get(TaskStatusEvent.class.getSimpleName(), TaskStatusEvent.class)
                .observe(this, event -> {
                    mPresenter.newsStatusChanged(event);
                });
    }


    @Override
    public void newsDoneSuccess(String taskId, String orderId) {
        LogUtils.e("newsDoneSuccess : "+taskId+" ,orderId = "+orderId);
    }

    @Override
    public void newsDoneFailed() {
        LogUtils.e("newsDoneFailed : failed ");
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mNewsView != null) {
            mNewsView.onRefresh();
        }

        if (mBannerView != null) {
            mBannerView.onRefresh();
        }
    }

}
